﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Talentnet.Web.UI;
using Talentnet.Data.Access;
using System.Collections.Generic;

using Col = Column<string, System.Type, int>;
using Telerik.Web.UI;
using Talentnet.Core.Utilities;
using Talentnet.Core.Utilities.Cryptophy;
using Talentnet.Core.Web;
using Talentnet.Core.Utilities.Mail;

[PagePermission("ManUsr")]
[PageTextResource("ManageUser")]
public partial class admin_ManageUser : NormalPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void grdUserList_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        grdUserList.DataSource = this.DBUser.GetUserList(SessionManager.CurrentUser.ClientID, (int)SiteEnums.UserType.Consultants);
    }

    protected void grdUserList_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            //Format DataItem
            GridDataItem dataItem = e.Item as GridDataItem;

            //UserName
            string loginName = dataItem["LoginName"].Text;
            string url = "UpdateUser.aspx?" + BuildSecureQueryString("u=" + loginName);
            dataItem["LoginName"].Text = string.Format("<a href='javascript:void(0)' onclick=\"OpenModalWindow(1,'{0}',500,300)\">{1}</a>", url, loginName);
        }
    }

    protected void DeleteUser(object sender, CommandEventArgs e)
    {
        string loginName = e.CommandArgument.ToString();
        //Delete roles
        this.DBUser.DeleteUsersInRoles(SessionManager.CurrentUser.ClientID, loginName);

        //Delete users
        this.DBUser.DeleteUser(SessionManager.CurrentUser.ClientID, loginName);
        //Reload users list
        grdUserList.Rebind();
    }

    protected void ResetPassword(object sender, CommandEventArgs e)
    {
        string loginName = e.CommandArgument.ToString();
        //Reset password
        SEC_User user = this.DBUser.GetUser(SessionManager.CurrentUser.ClientID, loginName);
        if (user != null)
        {
            user.UserPassword = CryptoAgent.Current.MD5(Common.GenPwd());
            user.UserPasswordType = (int)SiteEnums.PasswordType.SysPwd;
            user.UserPasswordDTS = DateTime.Now;
            this.DBUser.UpdateUser(user);

            //Send Email
            string fromEmail = SessionManager.MailSetting.EmailContact;
            //Load mail template
            System.IO.StreamReader mailTemplate = new System.IO.StreamReader(MapPath(@"~\App_Data\EmailTemplates\ActiveAccount.htm"));
            string mail_body = mailTemplate.ReadToEnd();
            string subject = "[Talentnet] - Kich Hoat Tai Khoan";
            mailTemplate.Close();
            mail_body = mail_body.Replace("[username]", user.LoginName);
            string link = Request.Url.AbsoluteUri.Replace(@"admin/ManageUser.aspx", @"freeaccess/ActiveAccount.aspx?") + BuildStaticQueryString("k=" + user.UserPassword);
            mail_body = mail_body.Replace("[link]", link);
            MailServices.SendEmail(fromEmail, user.Email, subject, mail_body, SessionManager.MailSetting);

            ((IMasterPage)Page.Master).AjaxAddResponseScript("alert('Kích hoạt thành công');");
        }
    }

    protected void RebindUserList(object sender, EventArgs e)
    {
        grdUserList.Rebind();
    }


}
